<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>评论列表</title>
  <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./css/iconfont.css">
  <link rel="stylesheet" href="./css/main.css">
  <link rel="stylesheet" href="./libs/layui/css/layui.css">
  <link rel="stylesheet" href="./libs/layui/css/layui.mobile.css">
  <link rel="stylesheet" href="./libs/layui/css/modules/layer/default/layer.css">
  <script src="./libs/jquery/jquery.min.js"></script>
  <script src="./libs/layui/layui.js"></script>
  <script src="./libs/art-template/template-web.js"></script>
  <script src="./libs/layui/lay/modules/layer.js"></script>
</head>

<body>
  <div class="container-fluid">
    <div class="common_title">
      评论列表
    </div>
    <div class="container-fluid common_con">
      <table class="table table-striped table-bordered table-hover mp20">
        <thead>
          <tr>
            <th>作者</th>
            <th>评论</th>
            <th>评论在</th>
            <th>提交于</th>
            <th>状态</th>
            <th class="text-center" width="100">操作</th>
          </tr>
        </thead>
        <tbody id="tbody">
          <tr class="danger">
            <td>中周</td>
            <td>How are you?</td>
            <td>《世界，你好》</td>
            <td>2017-07-09 18:22:00</td>
            <td>待审核</td>
            <td class="text-center">
              <a href="javascript:void(0);;" class="btn btn-info btn-xs">批准</a>
              <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
            </td>
          </tr>
        </tbody>
      </table>
      <div class="row text-center" id="test1">

      </div>
    </div>
  </div>
</body>
<script src=" ./js/http.js">
</script>
<script id="comments" type="text/html">
  {{each data}}
  {{if $value.state=='已拒绝'}}
  <tr class="danger">
    {{else}}
    <tr>
  {{/if}}
    <td>{{$value.author}}</td>
    <td>{{$value.content}}</td>
    <td>{{$value.title}}</td>
    <td>{{$value.date}}</td>
    <td>{{$value.state}}</td>
    <td class="text-center">
      {{if $value.state=='已通过'}}
      <a href="javascript:void(0);;" class="btn btn-warning btn-xs" onclick='refused({{$value.id}})'>拒绝</a>
      {{else if $value.state=='已拒绝'}}
      <a href="javascript:void(0);;" class="btn btn-info btn-xs" onclick='approval({{$value.id}})'>批准</a>
      {{else}}
      <a href="javascript:void(0);;" class="btn btn-warning btn-xs" onclick='refused({{$value.id}})'>拒绝</a>
      <a href="javascript:void(0);;" class="btn btn-info btn-xs" onclick='approval({{$value.id}})'>批准</a>
      {{/if}}
      <a href="javascript:void(0);;" class="btn btn-danger btn-xs"  onclick='dele({{$value.id}})'>删除</a>
    </td>
  </tr>
  {{/each}}
</script>
<script>
  $(function () {
    params();
  });

  let h_id = 0
  function params(curr, limit) {
    h_id = curr;
    let page = curr;
    let perpage = limit
    let arr = {
      page: page,
      perpage: perpage
    }
    mylink('get', '/admin/comment/search', arr, function (resData) {
      let get_comments = template('comments', resData.data);
      $('#tbody').html(get_comments);
      fen(resData.data.totalCount, curr)
    });
  }

  function fen(count, curr) {
    layui.use('laypage', function () {
      var laypage = layui.laypage;

      laypage.render({
        elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
        , count: count //数据总数，从服务端得到
        , curr: curr
        , limit: 6
        , jump: function (obj, first) {
          if (!first) {
            params(obj.curr, obj.limit)
          }
        }
      });
    });
  }

  function refused(c_id) {
    mylink('post', '/admin/comment/reject', { id: c_id }, function (resData) {
      if (resData.code != 200) {
        layer.msg(resData.msg);
        return;
      }
      params(h_id);
    });
  }
  function approval(c_id) {
    mylink('post', '/admin/comment/pass', { id: c_id }, function (resData) {
      if (resData.code != 200) {
        layer.msg(resData.msg);
        return;
      }
      params(h_id);
    });
  }
  function dele(c_id) {
    layer.confirm('确定删除吗', function (index) {
      //do something
      mylink('post', '/admin/comment/delete', { id: c_id }, function (resData) {
        if (resData.code != 200) {
          layer.msg(resData.msg);
          return;
        }
        params(h_id);
      });
      layer.close(index);
    });

  }
</script>

</html>